require 'json_web_token'

class Api::V1::UsersController < Api::V1::BaseController
  skip_before_action :authenticate_request!, only: [:create, :reset_password, :share]

  def show
    if @current_user.id == params[:id]
      @user = @current_user
      @is_self = true
    else
      @user = User.find params[:id]
      @is_self = false
    end
  end

  def create
    user = User.find_by_phone params[:phone]
    if user.blank?
      temp_user = TempUser.where(phone: params[:phone], action_type: 'register').order("created_at DESC").first
      if temp_user.blank?
        render json: { result: false, message: '请先获取验证码.' }, status: :ok
      else
        if temp_user.identify_code == params[:code]
          if params[:password] == params[:confirm_password]
            @user = User.new
            @user.phone = params[:phone]
            @user.password = params[:password]
            @user.sex = :female
            @user.state = :pending
            if @user.save
              render json: { result: false, message: "注册成功,等待审核.", data: { user_id: @user.id } }, status: :ok
            else
              render json: { result: false, message: render_errors(@user.errors.messages) }, status: :ok
            end
          else
            render json: { result: false, message: '两次密码不相同.' }, status: :ok
          end
        else
          render json: { result: false, message: '验证码错误.' }, status: :ok
        end
      end
    else
      render json: { result: false, message: '电话已注册.' }, status: :ok
    end
  end

  def update
    if params[:user][:password].presence
      if not @current_user.authenticate(params[:user][:password])
        render json: { result: false, message: '密码验证失败.' }, status: :ok and return
      end
      if not params[:user][:new_password] == params[:user][:confirm_password]
        render json: { result: false, message: '新密码两次输入不相同.' }, status: :ok and return
      end
      params[:user][:password] = params[:user][:new_password]
    end
    
    if @current_user.update user_params
      @user = @current_user
    else
      render json: { result: false, message: '更新错误.' }, status: :ok
    end
  end

  def reset_password
    temp_user = TempUser.where(phone: params[:phone], action_type: 'password').order("created_at DESC").first
    if temp_user.blank?
      render json: { result: false, message: '请先获取验证码.' }, status: :ok and return
    else
      if temp_user.identify_code == params[:code]
        if not params[:password] == params[:confirm_password]
          render json: { result: false, message: '新密码两次输入不相同.' }, status: :ok and return
        end
        user = User.find_by_phone(params[:phone])
        if user.blank?
          render json: { result: false, message: '该用户不存在.' }, status: :ok
        else
          if user.update_attributes(password: params[:password])
            render json: { result: true, message: '更新成功.' }, status: :ok
          else
            render json: { result: false, message: '更新错误.' }, status: :ok
          end
        end
      else
        render json: { result: false, message: '验证码错误.' }, status: :ok and return
      end
    end
  end

  def reset_phone
    temp_user = TempUser.where(phone: params[:phone], action_type: 'phone').order("created_at DESC").first
    if temp_user.blank?
      render json: { result: false, message: '请先获取验证码.' }, status: :ok and return
    else
      if temp_user.identify_code == params[:code]
        if @current_user.update_attributes(phone: params[:phone])
          render json: { result: true, message: '更新成功.' }, status: :ok
        else
          render json: { result: false, message: '更新错误.' }, status: :ok
        end
      else
        render json: { result: false, message: '验证码错误.' }, status: :ok and return
      end
    end
  end

  def share
    user = User.find_by_phone params[:phone]
    if user.blank?
      @user = User.new
      @user.phone = params[:phone]
      password = rand(999999).to_s
      @user.password = password
      coupon = Coupon.find_by_name('分享赠券')
      if @user.save
        user_coupon = UserCoupon.create(
          user_id: @user.id,
          coupon_id: coupon.id,
          state: :unused
        ) if coupon.presence
        @shared_user = User.find_by_id params[:uid]
        if @shared_user.presence
          @user.update(parent_id: @shared_user.id)
          user_coupon = UserCoupon.create(
            user_id: @shared_user.id,
            coupon_id: coupon.id,
            state: :unused
          ) if coupon.presence
        end
        @reponse = Qcloud::Sms.single_sender(@user.phone, "268467", ["新会员", "#{password}"])
        render json: { result: true, message: '注册成功.' }, status: :ok
      else
        render json: { result: false, message: '注册失败.' }, status: :ok
      end
    else
      render json: { result: false, message: '该手机已注册.' }, status: :ok
    end
  end

  private

  def user_params
    params.require(:user).permit(:phone, :nickname, :sex, :area, :avatar, :introduce, :password)
  end

end
